<?php

/**
 * This is the model class for table "menu".
 *
 * The followings are the available columns in table 'menu':
 * @property integer $id
 * @property integer $id_padre
 * @property string $link
 * @property string $titulo
 * @property integer $nivel
 * @property integer $indice
 * @property integer $publicado
 *
 * The followings are the available model relations:
 * @property Menu $idPadre
 * @property Menu[] $menus
 * @property Modulo[] $modulos
 */
class Menu extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'menu';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('titulo', 'required'),
			array('id_padre, nivel, indice', 'numerical', 'integerOnly'=>true),
			array('link', 'length', 'max'=>1024),
			array('titulo', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_padre, link, titulo, nivel, indice, publicado', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idPadre' => array(self::BELONGS_TO, 'Menu', 'id_padre'),
			'menus' => array(self::HAS_MANY, 'Menu', 'id_padre'),
			'modulos' => array(self::MANY_MANY, 'Modulo', 'modulo_menu(id_menu, id_modulo)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_padre' => 'Id Padre',
			'link' => 'Link',
			'titulo' => 'Titulo',
			'nivel' => 'Nivel',
			'indice' => 'Indice',
			'publicado' => 'Publicado',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);	
		$criteria->compare('titulo',$this->titulo,true);
		$criteria->compare('identificador',$this->identificador);
	

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function buscarHijos($id_padre){
		$id_padre=isset($id_padre)?$id_padre:$this->id;
		$sql="select m.* from menu m
			  inner join menu p on p.id=m.id_padre
			  where p.id=:id_padre";
		$connection=Yii::app()->db;
		$command=$connection->createCommand($sql);
		$command->bindParam(":id_padre",$id_padre);
		return $command->queryAll();
	}
	
	public function buscarHijosARPorIdentificador($identificador){
		$criteriaBusquedaHijos=new CDbCriteria(array(
				'condition'=>"mp.identificador=:identificador",
				'params'=>array(':identificador'=>$identificador),
				'alias'=>'m',
				'join'=>'inner join menu mp on mp.id=m.id_padre')
		);
		return Menu::model()->findAll($criteriaBusquedaHijos);
	}
	
	public function  buscarHijosARPorPk($id){
		$criteriaBusquedaHijos=new CDbCriteria(array(
				'condition'=>"mp.id=:id",
				'params'=>array(':id'=>$id),
				'alias'=>'m',
				'join'=>'inner join menu mp on mp.id=m.id_padre')
		);
		return Menu::model()->findAll($criteriaBusquedaHijos);
	}
}